package com.ebelter.btcomlib.utils;


import android.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;


//字符串加密解密工具类
public class StringEncryptDecryptUtils {

    public static final String PASSWORD_ENC_SECRET = "ZengcaiqingPhone=13826672468";
    /**
     * 加密
     **/
    private String encryptStr(String clearText) {
        try {
            DESKeySpec keySpec = new DESKeySpec(
                    MD5Utils.getMd5(PASSWORD_ENC_SECRET).getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey key = keyFactory.generateSecret(keySpec);

            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            String encrypedPwd = Base64.encodeToString(cipher.doFinal(clearText
                    .getBytes("UTF-8")), Base64.DEFAULT);
            return encrypedPwd;
        } catch (Exception e) {
        }
        return clearText;
    }


    /**
     * 解密
     **/
    private String decryptStr(String encryptedPwd) {
        try {
            DESKeySpec keySpec = new DESKeySpec(MD5Utils.getMd5(PASSWORD_ENC_SECRET).getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey key = keyFactory.generateSecret(keySpec);

            byte[] encryptedWithoutB64 = Base64.decode(encryptedPwd, Base64.DEFAULT);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] plainTextPwdBytes = cipher.doFinal(encryptedWithoutB64);
            return new String(plainTextPwdBytes);
        } catch (Exception e) {
        }
        return encryptedPwd;
    }



}
